/**
 * @author LARAKI Meryem, MATHIEU Renaud
 * <h1>ESIAL 2A - PIDR 2010 </h1>
 * <h2>Modelisation de connaissances et competences informatiques</h2>
 */

package graphique;


import java.awt.Component;
import java.awt.Dimension;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;


import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;

import org.semanticweb.owlapi.reasoner.NodeSet;

import modele.Parser;

public class VueAfficherClassesOntologie extends JPanel {
	private static final long serialVersionUID = 1L;
	
	//--------------------
	// Attributs
	//--------------------
	public Parser parser;
	
	private JTree arbre;
	private DefaultMutableTreeNode racine;
	private DefaultTreeModel treeModel;
	
	//--------------------
	// Constructeurs
	//--------------------
	public VueAfficherClassesOntologie(Parser parser) {
		
		this.parser = parser;
		
		
		this.racine = new DefaultMutableTreeNode(parser.getThing());
		initArbre(this.racine);
		
		// Creation du Modele
		treeModel = new DefaultTreeModel(racine);
		
		// Creation du JTree
		arbre = new JTree(this.racine);
		arbre.setModel(treeModel);
		arbre.setEditable(true);
		arbre.setCellRenderer(treeCellRenderer);
		
		// Ajout des elements graphiques
		JScrollPane scroll = new JScrollPane(arbre);
		scroll.setPreferredSize(new Dimension(250, 250));
		add(scroll);
		
		// Options du JPanel
		TitledBorder afact = BorderFactory.createTitledBorder("Classes");
		afact.setTitleJustification(TitledBorder.CENTER);
		setBorder(afact);
	}//cons-1

	// --------------------
	// Methodes
	// --------------------
	public void initArbre(DefaultMutableTreeNode racine) {
		
		// Pour tous les fils de la root
		NodeSet<OWLClass> rootSubClasses = parser.getReasoner().getSubClasses(parser.getThing(), true);
		for(OWLClass cls : rootSubClasses.getFlattened()) {
			
			DefaultMutableTreeNode courant = new DefaultMutableTreeNode(cls);
			
			// On ajoute aussi les fils des fils (s'il y en a)
			for(OWLClassExpression subcls : parser.getChildren(cls)) {
				DefaultMutableTreeNode node = new DefaultMutableTreeNode(subcls);
				courant.add(listFile((OWLClass)subcls, node));
			}
			
			this.racine.add(courant);
		}
		
	}
	
	/**
	 * Cette methode privee permet de recuperer les fils d'un noeud
	 * @param uneClasse OWLClass sur laquelle on veut recuperer les fils
	 * @param node le noeud de la classe
	 * @return le noeud remplit avec les fils
	 */
	private DefaultMutableTreeNode listFile(OWLClass uneClasse, DefaultMutableTreeNode node){
		for (OWLClassExpression sousClasse : parser.getChildren(uneClasse)) {
			DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(sousClasse);
			node.add(this.listFile((OWLClass)sousClasse, subNode));
		}
		return node;
	}

	TreeCellRenderer treeCellRenderer = new DefaultTreeCellRenderer(){
		private static final long serialVersionUID = 1L;

		public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel,boolean expanded, boolean leaf, int row, boolean hasFocus ) {
			super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus );
			
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
			if (node.getUserObject() instanceof OWLClass) {
				OWLClass courant = (OWLClass)node.getUserObject();
				String oldName = courant.toString();
				if(oldName.contains("#")) {
					String newName = oldName.split(".*#")[1];
					setText(newName.substring(0,newName.length()-1));
				}
			}
			return this;
		}

		
	};
	
}
